/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             *
 *              GNU Lesser General Public Licence (LGPL) version 3,             *
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
#ifndef HIMUSICFITTINGTASK_H
#define HIMUSICFITTINGTASK_H

#include "HiMusicWaveForm.h"
#include "FairTask.h"
#include "TF1.h"

class TClonesArray;

class HiMusicFittingTask : public FairTask
{
public:
  HiMusicFittingTask();
  ~HiMusicFittingTask();

  /** Initiliazation of task at the beginning of a run **/
  virtual InitStatus Init();

  /** ReInitiliazation of task when the runID changes **/
  virtual InitStatus ReInit();

  /** Executed for each event. **/
  virtual void Exec(Option_t *opt);

  /** Load the parameter container from the runtime database **/
  virtual void SetParContainers();

  /** Finish task called at the end of the run **/
  virtual void Finish();

  void SetFitType(TString type) { fitType = type; }
  void SetFittingTao(float tao) { fittingTao = tao; }
  void SetDebug(bool dbg) { debugMode = dbg; }


private:
  /** Input array from previous already existing data level **/
  TClonesArray *fHiMusicMappedArray;

  /** Output array to  new data level**/
  TClonesArray *fMusicFittingWaveDataArray;

  uint64_t ts = 0;             // 时间片编号
  uint64_t ts_pre = 0;         // 上一个时间片编号

  TString fitType = "lineExp";
  float fittingTao = 1000;
  bool debugMode = false;

  TClonesArray *FitPeaks(HiMusicWaveForm &wave);
  void FindPeaks(HiMusicWaveForm &wave, std::vector<int> &peakPositions, int &nPeaks, float &baseline, float &noise_level);
  HiMusicWaveForm CreateWave(TF1 *f, int np, int dt); //// pedestal = 0

  static double biExp(double *x, double *par);
  static double lineExp(double *x, double *par);
  static double singleExp(double *x, double *par);

  // HiMusicFittingTask(const HiMusicFittingTask &);
  // HiMusicFittingTask operator=(const HiMusicFittingTask &);

  ClassDef(HiMusicFittingTask, 1);
};

#endif // HIMUSICFITTINGTASK_H
